package main

import (
	"fmt"
	"math"
	"math/rand"
	"time"
)

/**
穷举法 穷举每一横行 每一竖行 求和最小差值
*/

func main() {
	startTime := time.Now().UnixNano()
	size := 19
	max := 100

	board := make([][]int, size)
	for k, _ := range board {
		board[k] = make([]int, size)
		for j, _ := range board[k] {
			board[k][j] = rand.Intn(max)
		}
	}
	result := getMinDiff(board, size)
	fmt.Println(result)
	endTime := time.Now().UnixNano()
	fmt.Println(endTime - startTime)
}

func getMinDiff(board [][]int, size int) int {
	minDiff := math.MaxInt64
	//穷举每一横行
	for i := 0; i < size; i++ {
		sumA := 0
		sumB := 0
		for k := 0; k < i; k++ {
			for c := 0; c < size; c++ {
				sumA += board[k][c]
			}
		}
		for j := i + 1; j < size; j++ {
			for c := 0; c < size; c++ {
				sumB += board[j][c]
			}
		}
		diff := int(math.Abs(float64(sumA) - float64(sumB)))
		if diff < minDiff {
			minDiff = diff
		}
	}

	//穷举每一竖列
	for i := 0; i < size; i++ {
		sumA := 0
		sumB := 0
		for k := 0; k < i; k++ {
			for r := 0; r < size; r++ {
				sumA += board[r][k]
			}
		}
		for j := i + 1; j < size; j++ {
			for r := 0; r < size; r++ {
				sumB += board[r][j]
			}
		}
		diff := int(math.Abs(float64(sumA) - float64(sumB)))
		if diff < minDiff {
			minDiff = diff
		}
	}
	return minDiff
}
